package org.pearl.springboot.minio.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.SpringProperties;
import org.springframework.http.MediaType;
import org.springframework.http.converter.*;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by TD on 2021/9/13
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");


    /**
     * 自定义消息转换器，覆盖默认
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        // 清理默认
        messageConverters.clear();
        // 添加
        messageConverters.add(new ByteArrayHttpMessageConverter());
        messageConverters.add(new StringHttpMessageConverter());
        messageConverters.add(new ResourceHttpMessageConverter());
        messageConverters.add(new ResourceRegionHttpMessageConverter());
        if (!shouldIgnoreXml) {
            messageConverters.add(new SourceHttpMessageConverter<>());
        }
        messageConverters.add(new AllEncompassingFormHttpMessageConverter());
        // 配置FastJson并添加
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes()); // 支持的MediaType
        fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8); // 默认字符集
        fastJsonHttpMessageConverter.setFastJsonConfig(getFastJsonConfig());
        messageConverters.add(fastJsonHttpMessageConverter);
    }


    /**
     * FastJson 配置
     */
    private FastJsonConfig getFastJsonConfig() {
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 配置序列化器功能
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty
        );
        // 序列化配置，将Long类型转为字符串
        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        serializeConfig.put(long.class, ToStringSerializer.instance);
        fastJsonConfig.setSerializeConfig(serializeConfig);
        return fastJsonConfig;
    }

    /**
     * 配置支持的媒体类型
     */
    private List<MediaType> getSupportedMediaTypes() {
        List<MediaType> supportedMediaTypes = Lists.newArrayList();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(new MediaType("application", "*+json"));
        return supportedMediaTypes;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
      //  registry.addInterceptor(new MvcPermissionInterceptor()).addPathPatterns("/**").excludePathPatterns("/v1/user/login");
      //  registry.addInterceptor(new MvcPermissionInterceptor222222222()).addPathPatterns("/**").excludePathPatterns("/v1/user/login");
    }




/*    @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
    @ConditionalOnMissingBean(MultipartResolver.class)
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setResolveLazily(false);
        return multipartResolver;
    }*/

/*    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }*/
}
